﻿using AutoMapper;
using Chat_IService;
using Chat_Model;
using Chat_Model.ViewModel;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace Chat_WebApi.Controllers.v1
{
    [Route("/api/v1/[controller]")]
    [ApiController]
    [Authorize]
    public class UsersController : Controller
    {

        private readonly ILarUsersService _larUsersService;
        private readonly ILarUsersFriendsService _larUsersFriendsService;
        private readonly ILarUsersFriendsApplyService _larUsersFriendsApplyService;
        private readonly IMapper _mapper;

        /// <summary>
        /// Initializes a new instance of the <see cref="UsersController"/> class.
        /// </summary>
        /// <param name="larUsersService"></param>
        /// <param name="larUsersFriendsService"></param>
        /// <param name="larUsersFriendsApplyService"></param>
        /// <param name="mapper"></param>
        public UsersController(
            ILarUsersService larUsersService,
            ILarUsersFriendsService larUsersFriendsService,
            ILarUsersFriendsApplyService larUsersFriendsApplyService,
            IMapper mapper)
        {
            this._larUsersService = larUsersService;
            this._larUsersFriendsService = larUsersFriendsService;
            this._larUsersFriendsApplyService = larUsersFriendsApplyService;
            this._mapper = mapper;
        }
        /// <summary>
        /// 获取我的信息
        /// </summary>
        [HttpGet("detail")]
        public async Task<MessageModel<object>> Detail()
        {
            LarUsers model = await this._larUsersService.GetUserInfoAsync();
            return new MessageModel<object>()
            {
                Code = 200,
                Data = model,
            };
        }
        /// <summary>
        /// 获取我的相关设置
        /// </summary>
        [HttpGet("setting")]
        public async Task<MessageModel<object>> Setting()
        {
            LarUsers model = await this._larUsersService.GetUserInfoAsync();
            LarUsersDto modelDto = this._mapper.Map<LarUsersDto>(model);
            return new MessageModel<object>()
            {
                Code = 200,
                Message = "success",
                Data = new
                {
                    User_info = modelDto,
                    Setting = new
                    {
                        theme_mode = "",//主题模式
                        theme_bag_img = "",//背景图片
                        theme_color = "",//主题颜色
                        notify_cue_tone = "",//是否消息通知
                        keyboard_event_notify = ""//键盘输入事件通知
                    }
                },
            };
        }
        /// <summary>
        /// 编辑我的信息
        /// </summary>
        [HttpPost("edit-user-detail")]
        public async Task<MessageModel<object>> EditUserDetail([FromForm] TalkDisturbParam param)
        {
            return new MessageModel<object>() { };
        }
        /// <summary>
        /// 修改头像 
        /// </summary>
        [HttpPost("edit-avatar")]
        public async Task<MessageModel<object>> EditAvatar([FromForm] TalkDisturbParam param)
        {
            return new MessageModel<object>() { };
        }
        /// <summary>
        /// 修改密码 
        /// </summary>
        [HttpPost("change-password")]
        public async Task<MessageModel<object>> ChangePassword([FromForm] TalkDisturbParam param)
        {
            return new MessageModel<object>() { };
        }
        /// <summary>
        /// 修改手机号 
        /// </summary>
        [HttpPost("change-mobile")]
        public async Task<MessageModel<object>> ChangeMobile([FromForm] TalkDisturbParam param)
        {
            return new MessageModel<object>() { };
        }
        /// <summary>
        /// 修改邮箱 
        /// </summary>
        [HttpPost("change-email")]
        public async Task<MessageModel<object>> ChangeEmail([FromForm] TalkDisturbParam param)
        {
            return new MessageModel<object>() { };
        }
        /// <summary>
        /// 发送邮箱验证码
        /// </summary>
        [HttpPost("send-change-email-code")]
        public async Task<MessageModel<object>> SendChangeEmailCode([FromForm] TalkDisturbParam param)
        {
            return new MessageModel<object>() { };
        }
        /// <summary>
        /// 用户查询接口
        /// </summary>
        [HttpPost("search-user")]
        public async Task<MessageModel<object>> SearchUser([FromBody] UsersSearchUserParam param)
        {
            LarUsers larUsers = await this._larUsersService.GetUserInfoAsync();
            if (param.User_id == 0 && string.IsNullOrWhiteSpace(param.Mobile)) return new MessageModel<object>() { Code = 305 };
            var model = await this._larUsersService.GetSingleAsync(x => x.Id == param.User_id || x.Mobile == param.Mobile);
            LarUsersFriends larUsersFriends = await this._larUsersFriendsService.GetSingleAsync(x => x.UserId == larUsers.Id && x.FriendId == model.Id);
            LarUsersFriendsApply larUsersFriendsApply = await this._larUsersFriendsApplyService.GetSingleAsync(x => (x.UserId == larUsers.Id && x.FriendId == model.Id) || (x.UserId == model.Id && x.FriendId == larUsers.Id));
            string nickname_remark = larUsersFriends?.Status > 0 ? larUsersFriends.Remark : "";
            if (model != null)
            {
                return new MessageModel<object>()
                {
                    Code = 200,
                    Message = "",
                    Data = new
                    {
                        id = model.Id,
                        mobile = model.Mobile,
                        nickname = model.Nickname,
                        nickname_remark = nickname_remark,
                        motto = model.Motto,
                        avatar = model.Avatar,
                        friend_status = (larUsersFriends?.Status ?? 0) + 1,
                        friend_apply = larUsersFriendsApply != null ? 1 : 0,
                        gender = model.Gender,
                    },
                };
            }
            else
            {
                return new MessageModel<object>()
                {
                    Code = 305,
                    Message = "查询失败...",
                    Data = null,
                };
            }
        }
    }
}
